/**
 * 
 */
package info.niwota.webime.http;


/**
 * @author qiangli
 *
 */
public class WebInputHandler implements Handler {

	private static final String TAG = "WebInputHandler";


	/**
	 * 
	 */
	public WebInputHandler() {
	}


	public boolean handle(Request req, Response res) throws Exception {
		res.statusCode = Response.CODE200;
		res.contentType = Response.TYPE_TEXT_HTML;
		res.lastModified = System.currentTimeMillis();
		res.date = System.currentTimeMillis();
		res.transferEncoding = null;
		
		final String title = "Hotweb Input Method";
		final String method = req.method;
		
		if (method != null && method.equalsIgnoreCase("POST")) {
			String input = req.getParam("input");
			Log.d(TAG, "input [" + input +"]");
			
			//
			//WebKeyboardService kbs = (WebKeyboardService) req.webContext.serverContext.context;
			//kbs.updateInput(1, input);
			//
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("<html><head><title>"+ title +"</title></head><body>");
			
			sb.append("<h3>Hotweb Input Method</h3>");
			
			sb.append("<form name=\"inputform\" action=\"/ime\" method=\"post\">");
			sb.append("<textarea cols=\"40\" rows=\"4\" name=\"input\"></textarea>");
			sb.append("<br />");
			sb.append("<input type=\"submit\" value=\"Enter\" />");
			sb.append("</form> ");
			
			sb.append("</body></html>");
			String s = sb.toString();
			
			if (Debug.DEBUG) {
				Log.d(TAG, s);
			}
			res.send(s);
		}
		return true;
	}

}
